import https from 'https';

// 禁用SSL证书验证（如果是自签名证书的话）
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

const options = {
    hostname: '112.192.16.22',
    port: 9444,
    path: '/api/webhooks/1b13f7b0-62c4-4c05-a8f2-ef4fa387a91e',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    }
};

function triggerCI() {
    return new Promise((resolve, reject) => {
        const req = https.request(options, (res) => {
            let data = '';

            res.on('data', (chunk) => {
                data += chunk;
            });

            res.on('end', () => {
                if (res.statusCode >= 200 && res.statusCode < 300) {
                    console.log('CI触发成功！');
                    resolve(data);
                } else {
                    reject(new Error(`请求失败，状态码: ${res.statusCode}, 响应: ${data}`));
                }
            });
        });

        req.on('error', (error) => {
            console.error('发送请求时出错:', error.message);
            reject(error);
        });

        // 发送空的请求体
        req.write(JSON.stringify({}));
        req.end();
    });
}

// 执行触发
triggerCI()
    .then(() => {
        process.exit(0);
    })
    .catch((error) => {
        console.error('执行失败:', error.message);
        process.exit(1);
    });